#include "customprogressbar.h"
#include <QDebug>
#include <QPainterPath>
CustomProgressBar::CustomProgressBar(QWidget *parent) : QProgressBar(parent)
{
}

void CustomProgressBar::paintEvent(QPaintEvent *event)
{
//    QProgressBar::paintEvent(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);

    painter.setBrush(QColor(76, 175, 80)); // 进度条颜色
    QPainterPath path;

    h =  height() * (value() - minimum()) / (maximum() - minimum()); //音量进度条的高度
//    qDebug()<<"2  重绘时的 value 值  :" << this->value();
//    qDebug()<<"2  重绘时的 h 值  :" <<h;

    if(value() == 100 || value() == 0){
         painter.drawRoundedRect(0, height()-h , width() , h, 10, 10);
         return;
    }else if(value() <= 10){
        path = drawPath(1);
    }else if(value() <= 20){
        path = drawPath(2);

    }else{
        path = drawPath(3);
    }
    painter.drawPath(path);

}

QPainterPath CustomProgressBar::drawPath(int radius)
{

    double radiusControl = 0;

    if (radius == 2){
        radiusControl = 1;
    }

    if (radius == 1){
        radiusControl = 0.5;
    }

    QPainterPath path;
    path.moveTo(0, height() - h);               // 移动到左上角
    path.lineTo(width(), height() - h);         // 向右横线到右上角
    path.lineTo(width(), height() - radius);    // 向下竖线到右下边
    path.quadTo(width() - radiusControl, height() - radiusControl, width() - radius, height()); // 右下角圆角
    path.lineTo(radius, height());              // 向左直线到左下角起始点
    path.quadTo(0 + radiusControl, height() - radiusControl, 0, height() - radius);             // 左下角圆角
    path.lineTo(0, height() - h);               // 向上直线回到左上角

    return path;
}
